package br.com.j7.jdesk.persistence.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import br.com.j7.jdesk.persistence.listener.UserListener;

/**
 * Entidade Users
 * 
 * @author Edivando Alves
 *
 */
@Entity
@EntityListeners(value= {UserListener.class} )
@NamedQueries({ @NamedQuery(name="user", query="Select u from Users u"),
				@NamedQuery(name="userByEnterprise", query="Select user FROM Users user INNER JOIN FETCH user.userGroup ug INNER JOIN ug.enterprise where ug.enterprise = ?1"),
				@NamedQuery(name="userByEnterpriseAndCdLogin", query="Select user FROM Users user INNER JOIN FETCH user.userGroup ug INNER JOIN ug.enterprise e where e.cdEnterprise = ?1 AND user.cdLogin = ?2"),
				@NamedQuery(name="userByCdLogin", query="select u from Users u where u.cdLogin = ?1"),
				@NamedQuery(name="userByUserAndEnterprise", query="FROM Users user INNER JOIN FETCH user.userGroup ug where ug.enterprise.cdEnterprise = ?1 AND user.idUser = ?2")})
public class Users implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idUser;
	
	@ManyToOne
	@JoinColumn(name="idUserGroup")
	private UsersGroups userGroup;
	
	@JoinColumn(name="idUser")
	@OneToMany(mappedBy="user", cascade = CascadeType.PERSIST)
	private List<Services> services;
	
	@Column(length=70, nullable=true)
	private String cdUser;
	@Column(length=50, nullable=true)
	private String cdLogin;
	@Column(nullable=true)
	private String cdPassword;
	@Column(nullable=true)
	private String cdConfirmPassword;
	@Column(length=70)
	private String cdEmail;
	@Column(length=20)
	private String cdPhone;
	
	private Integer age;
	
	@OneToMany(mappedBy="user", cascade=CascadeType.MERGE)
	private List<ServicesLogs> servicesLogs;
	
	@OneToMany(mappedBy="user", cascade=CascadeType.MERGE)
	private List<ServicesAnnotations> servicesAnnotations;
	
	@OneToMany(mappedBy="user", cascade=CascadeType.MERGE)
	private List<Sales> sales;
	
	@Version
	private Integer nrVersion;
	
	public Users() {
	}
	
	public Users(Integer idUser) {
		this.idUser = idUser;
	}
	
	
	public Integer getIdUser() {
		return idUser;
	}
	
	public void setIdUser(Integer idUser) {
		this.idUser = idUser;
	}
	
	public String getCdUser() {
		return cdUser;
	}
	
	public void setCdUser(String cdName) {
		this.cdUser = cdName;
	}

	public String getCdLogin() {
		return cdLogin;
	}

	public void setCdLogin(String cdLogin) {
		this.cdLogin = cdLogin;
	}

	public String getCdPassword() {
		return cdPassword;
	}

	public void setCdPassword(String cdPassword) {
		this.cdPassword = cdPassword;
	}
	
	public String getCdConfirmPassword() {
		return cdConfirmPassword;
	}
	
	public void setCdConfirmPassword(String cdConfirmPassword) {
		this.cdConfirmPassword = cdConfirmPassword;
	}
	
	public String getCdEmail() {
		return cdEmail;
	}

	public void setCdEmail(String cdEmail) {
		this.cdEmail = cdEmail;
	}

	public String getCdPhone() {
		return cdPhone;
	}
	
	public void setCdPhone(String cdPhone) {
		this.cdPhone = cdPhone;
	}

	public void setUserGroup(UsersGroups userGroup) {
		this.userGroup = userGroup;
	}
	
	public UsersGroups getUserGroup() {
		return userGroup;
	}
	
	public List<Services> getServices() {
		if(services == null){
			services = new ArrayList<>();
		}
		return services;
	}
	
	public void setServices(List<Services> services) {
		this.services = services;
	}
	
	public List<Sales> getSales() {
		return sales;
	}
	
	public void setSales(List<Sales> sales) {
		this.sales = sales;
	}
	
	public Integer getNrVersion() {
		return nrVersion;
	}
	
	public void setNrVersion(Integer nrVersion) {
		this.nrVersion = nrVersion;
	}	
	
	public Integer getAge() {
		return age;
	}
	
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public List<ServicesLogs> getServicesLogs() {
		return servicesLogs;
	}
	
	public void setServicesLogs(List<ServicesLogs> servicesLogs) {
		this.servicesLogs = servicesLogs;
	}
	
	public List<ServicesAnnotations> getServicesAnnotations() {
		return servicesAnnotations;
	}
	
	public void setServicesAnnotations(List<ServicesAnnotations> servicesAnnotations) {
		this.servicesAnnotations = servicesAnnotations;
	}
	
	@Override
	public int hashCode() {
		return idUser;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Users){
			Users usuario = (Users) obj;
			return this.getCdUser().equalsIgnoreCase(usuario.getCdUser()) &&
					this.getCdLogin().equals(usuario.getCdLogin());
		}
		return false;
	}
	
	@Override
	public String toString() {
		return cdUser;
	}
}
